package com.woniu40.petai_manager.service;

import com.woniu40.petai_manager.entity.Manager;
import com.woniu40.petai_manager.entity.Permission;
import com.woniu40.petai_manager.entity.Role;
import com.woniu40.petai_manager.mapper.ManagerMapper;
import com.woniu40.petai_manager.mapper.RoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu40.petai_manager.entity.Manager;
import com.woniu40.petai_manager.mapper.ManagerMapper;
import com.woniu40.petai_manager.mapper.RoleMapper;
import com.woniu40.petai_manager.vo.ManagerVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ManagerService {
    @Autowired
    ManagerMapper managerMapper;

    @Autowired
    RoleMapper roleMapper;

    public Manager findByName(String userName){
        return managerMapper.selectByPrimaryName(userName);
    }

    public List<Role> findByPrimaryKey(Integer managerId){
        return roleMapper.selectByManagerId(managerId);
    }

    public List<Permission> findByRoleId(Integer roleIds){
        return managerMapper.selectByRoleId(roleIds);
    }


    public PageInfo<Manager> findManagerByPage(Integer pageNum,Integer pageSize,String userName){
        PageHelper.startPage(pageNum,pageSize);
        List<Manager> managerList = managerMapper.selectManagerByPage(userName);
        return new PageInfo<>(managerList);
    }
    @Transactional
    public int addManager(ManagerVo managerVo){
        int insert = managerMapper.insert(managerVo);
        Integer managerId = managerVo.getManagerId();
        if(managerVo.getRolelist().size()!=0){
            int i= managerMapper.insertManagerRole(managerId,managerVo.getRolelist(),managerVo.getInsertTime());

        }
        return insert;

    }
    @Transactional
    public int deleteManager(Integer id){
        int i= managerMapper.deleteManagerAndRole(id);
        return managerMapper.deleteByPrimaryKey(id);
    }
   /* public int modifyManager(Manager manager){
        return managerMapper.updateByPrimaryKey(manager);
    }*/
    public  ManagerVo findManagerById(Integer managerId){
        return managerMapper.selectByMid(managerId);
    }


   /* public ManagerVo findManagerVo(Integer managerId) {
        ManagerVo managerVo = managerMapper.selectManagerVo(managerId);
        return managerVo;
    }*/
    @Transactional
    public int modifyManagerAndRole(ManagerVo managerVo) {
        int i = managerMapper.updateManager(managerVo);
        managerMapper.deleteManagerAndRole(managerVo.getManagerId());
        if(managerVo.getRolelist().size()!=0) {
            managerMapper.insertManagerRole(managerVo.getManagerId(), managerVo.getRolelist(), managerVo.getUpdateTime());
        }
        return i;
    }

    public List<Integer> findRoleByMid(Integer managerId) {
        return roleMapper.selectRoleIdByManager(managerId);
    }
}
